package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName DictController
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/5/30 12:43
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/5/30 12:43
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */

@Slf4j
@Api(value = "数据字典接口")
@RestController
//@CrossOrigin //解决跨域问题
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;


    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }


    /**
     * 导出数据字典
     *
     * @return
     */
    @ApiOperation(value = "导出数据字典")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    /**
     * 导入数据字典
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入数据字典")
    @PostMapping("importData")
    public void importData(MultipartFile file) {
        dictService.importDictData(file);
    }

    /**
     * 根据字典编码和值获取名称
     *
     * @param dictCode
     * @param value
     * @return
     */
    @ApiOperation(value = "根据字典编码和值获取名称")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode, @PathVariable("value") String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    /**
     * 根据字典值获取名称
     *
     * @param value
     * @return
     */
    @ApiOperation(value = "根据字典值获取名称")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable("value") String value) {
        String dictName = dictService.getDictName(null, value);
        return dictName;
    }

    /**
     * 根据数据dictCode查询子数据数据详情
     *
     * @param dictCode
     * @return
     */
    @ApiOperation(value = "根据数据dictCode查询子数据数据详情")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode) {
//        log.info("dictCode: " + dictCode);
        List<Dict> list = dictService.findByDictCode(dictCode);
//        log.info("list: " + list);
        return Result.ok(list);
    }
}
